package com.cr.crandroid.ui.person.approve;

import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.gridlayout.widget.GridLayout;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.cr.crandroid.R;
import com.cr.crandroid.bean.Car;
import com.cr.crandroid.vo.CarVo;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;


public class ApproveDetailFragment extends Fragment {

    private CarVo car = new CarVo();

    private GridLayout gridLayout;
    private List<ImageView> imageViews;
    private ImageView imageView;
    private ImageView imageView2;
    private ImageView imageView3;
    private ImageView imageView4;


    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState, Context context) {
        View root = inflater.inflate(R.layout.fragment_approve_detail, container, false);
        TextView textView3 = root.findViewById(R.id.textView3);
        TextView textView6 = root.findViewById(R.id.textView6);
        TextView textView8 = root.findViewById(R.id.textView8);
        TextView textView10 = root.findViewById(R.id.textView10);
        TextView textView12 = root.findViewById(R.id.textView12);

        gridLayout = root.findViewById(R.id.gridLayout);

        imageView = root.findViewById(R.id.imageView);
        imageView2 = root.findViewById(R.id.imageView2);
        imageView3 = root.findViewById(R.id.imageView3);
        imageView4 = root.findViewById(R.id.imageView4);

        imageView.setVisibility(View.GONE);
        imageView2.setVisibility(View.GONE);
        imageView3.setVisibility(View.GONE);
        imageView4.setVisibility(View.GONE);

        imageViews = new ArrayList<>();
        imageViews.add(imageView);
        imageViews.add(imageView2);
        imageViews.add(imageView3);
        imageViews.add(imageView4);
        if (savedInstanceState != null && savedInstanceState.containsKey("car")) {
            String carJson = savedInstanceState.getString("car");
            car = new Gson().fromJson(carJson, new TypeToken<CarVo>() {
            }.getType());
            textView3.setText(car.getRealName());
            textView6.setText(car.getBrand());
//            textView8.setText(car.getLicensePlate());
            textView10.setText(car.getColor());
            textView12.setText(car.getRentalStart() + "至" + car.getRentalEnd());
        }
        if (car.getImage() != null) {
            String[] split = car.getImage().split(",");
            setImage(split.length);
            for (int i = 0; i < split.length; i++) {
                System.out.println(split[i]);
                Glide.with(context).load(split[i])
                        .into(imageViews.get(i));
            }
        }

        return root;
    }

    private void setImage(Integer size) {
        switch (size) {
            case 1:
                imageView.setVisibility(View.VISIBLE);
                gridLayout.removeView(imageView2);
                gridLayout.removeView(imageView3);
                gridLayout.removeView(imageView4);
                break;
            case 2:
                imageView.setVisibility(View.VISIBLE);
                imageView2.setVisibility(View.VISIBLE);
                gridLayout.removeView(imageView3);
                gridLayout.removeView(imageView4);
                break;
            case 3:
                imageView.setVisibility(View.VISIBLE);
                imageView2.setVisibility(View.VISIBLE);
                imageView3.setVisibility(View.VISIBLE);
                gridLayout.removeView(imageView4);
                break;
            case 4:
                imageView.setVisibility(View.VISIBLE);
                imageView2.setVisibility(View.VISIBLE);
                imageView3.setVisibility(View.VISIBLE);
                imageView4.setVisibility(View.VISIBLE);
                break;
        }
    }
}